package com.example.wxai.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.wxai.mapper.UserMapper;
import com.example.wxai.pijo.dto.UserDto;
import com.example.wxai.pijo.entity.User;
import com.example.wxai.pijo.vo.UserVo;
import com.example.wxai.result.PageResult;
import com.example.wxai.service.IUserService;
import com.example.wxai.utils.BeanCopyUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-12
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private UserMapper userMapper;
    @Override
    public void addEssayCount(String essayAuthorId) {
        UpdateWrapper<User> userUpdateWrapper=new UpdateWrapper<>();
        userUpdateWrapper.eq("user_id",essayAuthorId).setSql("reviews=reviews+1");
        userMapper.update(userUpdateWrapper);
    }

    /**
     * 管理端获得用户数据
     * @param userDto
     * @return
     */
    public PageResult<UserVo> pageList(UserDto userDto) {
        Page<User> page=Page.of(userDto.getPage(),userDto.getPageSize());
        lambdaQuery().eq(!StringUtils.isBlank(userDto.getUserId()),User::getUserId,userDto.getUserId())
                .eq(!StringUtils.isBlank(userDto.getUserName()),User::getUserName,userDto.getUserName())
                .eq(userDto.getStatus()!=null,User::getStatus,userDto.getStatus()).page(page);
        PageResult<UserVo> pageResult=new PageResult<>();
        List<UserVo> userVos = BeanCopyUtils.copyListProperties(page.getRecords(), UserVo::new);
        pageResult.setList(userVos);
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }
}
